package fr.univ.surveyonline.model;

import java.util.List;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;

import fr.univ.surveyonline.datastore.PMF;
import fr.univ.surveyonline.model.allEnum.AgeEnum;
import fr.univ.surveyonline.model.allEnum.Categorie;
import fr.univ.surveyonline.model.allEnum.Region;
import fr.univ.surveyonline.model.allEnum.SexeEnum;

@PersistenceCapable(identityType = IdentityType.DATASTORE)
public class Contraintes {
	private ContraintesEntities[] required;
	
	// constructeur
	public Contraintes() {
		required = new ContraintesEntities[TOTAL_NUMBER];
	}
	
	@SuppressWarnings("unchecked")
	public Contraintes(Survey s) {
		this();
		if (s.getKey() == null) {
			return;
		}
		List<ContraintesEntities> list = (List<ContraintesEntities>) PMF.getAllFromClass(ContraintesEntities.class);
		for (ContraintesEntities c2 : list) {
			if (s.getKey().getId() == c2.getSurveyKey().getId()) {
				required[c2.getOrdinal()] = c2;
			}
		}
	}
	
	// METHODES
	public Integer getMinimum(Object o) {
		ContraintesEntities c = required[ordinal(o)];
		if (c == null) {
			return null;
		}
		return c.getMinimum();
	}
	
	public Double getMinimumPourCent(Object o) {
		ContraintesEntities c = required[ordinal(o)];
		if (c == null) {
			return null;
		}
		return c.getPourcent();
	}
	
	public boolean isRequired(Object o) {
		ContraintesEntities c = required[ordinal(o)];
		if (c == null) {
			return true;
		}
		return ((c.getMinimum() != null)||(c.getPourcent() != null));
	}
	
	public void setNotRequired(Object o) {
		ContraintesEntities c = required[ordinal(o)];
		if (c == null) {
			c = new ContraintesEntities(o);
			required[ordinal(o)] = c;
		} else {
			c.setMinimum(null);
			c.setPourcent(null);
		}
	}
	
	public void setIsRequired(Object o,Integer minimum, Double minimumProZent) {
		if ((minimum != null) || (minimumProZent != null)) {
			ContraintesEntities c = required[ordinal(o)];
			if (c == null) {
				c = new ContraintesEntities(o);
				required[ordinal(o)] = c;
			}
			c.setMinimum(minimum);
			c.setPourcent(minimumProZent);
		}
	}

	private static final int CATEGORIE =0,
								REGION = Categorie.values().length, 
								AGE =REGION + Region.values().length, 
								SEXE = AGE + AgeEnum.values().length,
								TOTAL_NUMBER = SEXE + SexeEnum.values().length;
	
	public static int ordinal(Object o) {
		if (o.getClass() == AgeEnum.class) {
			return AGE + ((AgeEnum) o).ordinal();
		} else if (o.getClass() == SexeEnum.class) {
			return SEXE + ((SexeEnum) o).ordinal();
		} else if (o.getClass() == Categorie.class) {
			return CATEGORIE + ((Categorie) o).ordinal();
		} else if (o.getClass() == Region.class) {
			return REGION + ((Region) o).ordinal();
		} else {
			throw new IllegalStateException("Erreur du programmeur");
		}
	}

	/*public boolean hasLeastOneContraintes() {
		for (int i=0; i <this.required.length;i++) {
			if (this.required[i] != null) {
				return true;
			}
		}
		return false;
	}*/
	
	public boolean isValid() {
		return isValid(Categorie.values()) && isValid(Region.values())
			&& isValid(AgeEnum.values()) && isValid(SexeEnum.values()); 
	}

	private boolean isValid(Object[] values) {
		double d = 0;
		for (Object o : values) {
			Integer newI = this.getMinimum(o); 
			if ((newI != null)&&(newI < 0)) {
				return false;
			}
			Double newD = this.getMinimumPourCent(o); 
			if (newD != null) {
				if (newD < 0) {
					return false;
				}
				d+=newD;
			}
		}
		return (d <= 100);
	}
	
	public void makePersistent(Survey s) {
		for (ContraintesEntities c : required) {
			if (c != null) {
				c.setSurveyKey(s);
				PMF.makePersistent(c);
			}
		}
	}
	
	public void deletePersistent() {
		for (ContraintesEntities c : required) {
			if (c != null) {
				PMF.delete(c);
			}
		}
	}
}
